const jwt = require("jwt-simple")

// 封装生成token
const jwtEncode = (adminName, secret) => {
  return jwt.encode({
    adminName,
    exp: Date.now() / 1000 + 5
  }, secret)
}

// 封装解码token
const jwtDecode = (token, secret) => {
  try {
    const { adminName } = jwt.decode(token, secret)
    // 如果解码成功,返回一个对象,对象的code状态为1,并且带有username
    return {
      code: 1,
      adminName
    }
  } catch (e) {
    // 如果解码出错,返回一个对象,code状态为0
    return {
      code: 0
    }
  }
}

module.exports = {
  jwtEncode,
  jwtDecode
}